var selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoTabPage();
        }
    }
});

function doRefresh() {
    selectedRow = '';
    var menuId = $('#menuId').val();
    var status = $('#status').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'tab/pageFindTabData?t=' + new Date().getTime(),
        query: {
            menuId : menuId,
            status : status
        },
    });
}

function getParams(params) {
    var menuId = $('#menuId').val();
    var status = $('#status').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        menuId : menuId,
        status : status,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openAddTabPage() {
    openEditPage('add', 0);
}

function openEditTabPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('edit', selectedRow.id);
}

function openInfoTabPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id);
}

function openEditPage(command, id) {
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'tab/editTabPage?type=' + command + '&id=' + id + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function changeState() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'enabled';
    if (selectedRow.status == 'enabled') {
        status = 'disabled';
    }
    var postData = {
        id : selectedRow.id,
        status : status
    }
    $.ajax({
        type: 'post',
        url: basePath + 'tab/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('修改成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function deleteTab() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var strHtml = '是否删除TAB页【'+selectedRow.tabName+'】？该操作会变更TAB页数据！';
    swalToastr('', strHtml, 'warning', doBtnDel);
}

function doBtnDel() {
    var postData = {
        id : selectedRow.id,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'tab/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function formatStatus(value, row, index){
    var title;
    if (value == 'enabled') {
        title = "有效";
    } else {
        title = "无效";
    }
    return title;
}

// dataTable
$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    // checkBox 的勾选
    $('#dataTable .success').removeClass('success');//去除之前选中的行的，选中样式
    $element.addClass('success');//添加当前选中的 success样式用于区别
    changeStatueBtn(row.status == 'enabled',"冻结","恢复");
    selectedRow = row;
});